package com.os.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.os.dao.RoleDaoM;
import com.os.util.JdbcConUtilM;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
 * 角色操作
 * @author MWX
 *
 */
public class RoleDaoImplM implements RoleDaoM{

	public JSONArray getFuncByUid(int uid, int pid) {
		JSONArray ja=new JSONArray();
		//获取当前用户功能
		String funcs="";
		StringBuffer sql=new StringBuffer("select auths from o_role");
		sql.append(" where rid in");
		sql.append(" (select rid from o_user");
		sql.append(" where uid=?)");
		
		Connection con=JdbcConUtilM.getCon();
		PreparedStatement pst=null;
		try {
			 pst=con.prepareStatement(sql.toString());
			 pst.setInt(1, uid);
			 ResultSet rs=pst.executeQuery();
			 if(rs.next()){
				 funcs=rs.getString("auths");
			 }
			 StringBuffer sql2=new StringBuffer("select * from o_auth");
				sql2.append(" where aid in");
				sql2.append(" ("+funcs+") and pid=?");
				PreparedStatement pst1=con.prepareStatement(sql2.toString());
				pst1.setInt(1, pid);
				ResultSet rs1=pst1.executeQuery();
				while(rs1.next()){
					JSONObject j=new JSONObject();
					j.put("id", rs1.getInt("aid"));
					//j.put("iconCls", "icon-ok");
					j.put("text", rs1.getString("aname"));
                    if(rs1.getString("astate").equals("closed")){
						j.put("iconCls", "icon-c");
					}else{
						j.put("iconCls", "icon-a");
					}
					j.put("state", rs1.getString("astate"));
					
					JSONObject attr=new JSONObject();
					attr.put("url", rs1.getString("aurl"));
					j.put("attributes", attr);
					ja.add(j);
				}
				
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JdbcConUtilM.closePstAndCon(pst, con);
		return ja;
	}

}
